import axios from 'axios';
import { Message } from 'element-ui';
// import qs from 'qs';

const successCode = 1;//接口成功响应状态码，具体以项目为准

// 创建axios实例
const service = axios.create({
    baseURL: 'http://localhost:8082/user', // api的base_url，以实际项目为准
    timeout: 5000, // 请求超时时间
});

// request拦截器
service.interceptors.request.use(config => {
    // Do something before request is sent
    // if (store.getters.token) {
    //     config.headers['token'] = getToken() // 让每个请求携带token--['token']为自定义key 请根据实际情况自行修改
    // }

    return config
}, error => {
    // Do something with request error
    console.log(error); // for debug
    return  Promise.reject(error)
});

// respone拦截器
service.interceptors.response.use(
    response => {
        const res = response.data;
        // if the custom code is not 20000, it is judged as an error.
        if (res.code !== successCode) {
            // Toast.fail(res.msg);
            // this.$message.error(res.message);
            return Promise.reject(new Error(res.msg || 'Error'))
        } else {
            return res
        }
    },
    error => {
        console.log('err' + error);// for debug
        // this.$message.error(error.message);
        return Promise.reject(error)
    });

export default service

/**
 * get 请求方法
 * @param url
 * @param params
 * @returns {Promise}
 */
export function get(url, params = {}) {
    return new Promise((resolve, reject) => {
        service
            .get(url, {
                params: params
            })
            .then(response => {
                resolve(response)
            })
            .catch(err => {
                reject(err)
            })
    })
}

/**
 * post 请求方法
 * @param url
 * @param data
 * @returns {Promise}
 */
export function post(url, data = {}) {
    return new Promise((resolve, reject) => {
        service.post(url, data).then(
            response => {
                // console.log(response.data.code)
                if(response.Code===0){
                    resolve(response)
                }else {
                    Message.error(response.Message)
                    reject(response)
                }
            },
            err => {
                reject(err)
            }
        )
    })
}
